package com.xh.petdogexpert.expert.service.imp;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.xh.petdogexpert.expert.dto.Disease;
import com.xh.petdogexpert.expert.dto.JudgeDiseaseLog;
import com.xh.petdogexpert.expert.dto.JudgeLog;
import com.xh.petdogexpert.expert.dto.JudgeSymptomLog;
import com.xh.petdogexpert.expert.mapper.DiseaseMapper;
import com.xh.petdogexpert.expert.mapper.JudgeDiseaseLogMapper;
import com.xh.petdogexpert.expert.mapper.JudgeLogMapper;
import com.xh.petdogexpert.expert.mapper.JudgeSymptomLogMapper;
import com.xh.petdogexpert.expert.service.IDiseaseService;
import com.xh.petdogexpert.expert.service.IJudgeLogService;
import com.xh.petdogexpert.user.dto.User;

@Service
public class JudgeLogServiceImp implements IJudgeLogService {

	@Autowired
	JudgeLogMapper judgeLogMapper;
	@Autowired
	JudgeDiseaseLogMapper judgeDiseaseLogMapper;
	@Autowired
	JudgeSymptomLogMapper judgeSymptomLogMapper;

	@Autowired
	HttpSession session;

	/**
	 * 首次选择症状，作为记录
	 * 
	 * @author linxixin
	 * @date 2016年4月11日
	 */
	public boolean log(List<Disease> diseaseSumCf, List<String> symptomIds) {
		User user = (User) session.getAttribute("user");
		JudgeLog log = new JudgeLog();
		log.setUserId(user.getId());
		judgeLogMapper.insertSelective(log);

		for (String id : symptomIds) {
			JudgeSymptomLog symptomLog = new JudgeSymptomLog();
			symptomLog.setSymptomId(Integer.parseInt(id));
			symptomLog.setJudgeId(log.getId());
			judgeSymptomLogMapper.insertSelective(symptomLog);
		}

		for (Disease d : diseaseSumCf) {
			JudgeDiseaseLog diseaseLog = new JudgeDiseaseLog();
			diseaseLog.setJudgeId(log.getId());
			diseaseLog.setDeseaseId(d.getId());
			diseaseLog.setSumcf(d.getSumCf());
			judgeDiseaseLogMapper.insertSelective(diseaseLog);
		}

		return true;
	}

	@Override
	public List<JudgeLog> getMyJudgeLogs(Integer pagenum, Integer pagesize) {
		User user = (User) session.getAttribute("user");
		JudgeLog judgeLog = new JudgeLog();
		judgeLog.setUserId(user.getId());
		
		return queryJudgeLogs(judgeLog, pagenum, pagesize);
	}

	@Override
	public List<JudgeLog> queryJudgeLogs(JudgeLog judgeLog, Integer pagenum, Integer pagesize) {
		PageHelper.startPage(pagenum, pagesize);
		return judgeLogMapper.queryJudgeLog(judgeLog);
	}
	
	@Autowired
	DiseaseMapper diseaseMapper;

	@Override
	public JudgeLog getMyJudgeLog(Integer judgeLogId) {
		JudgeLog judgeLog = new JudgeLog();
		judgeLog.setId(judgeLogId);
		List<JudgeLog> queryJudgeLog = judgeLogMapper.queryJudgeLog(judgeLog);
		
		judgeLog = queryJudgeLog.get(0);

		JudgeDiseaseLog judgeDiseaseLog = new JudgeDiseaseLog();
		judgeDiseaseLog.setJudgeId(judgeLogId);
		List<JudgeDiseaseLog> judgeDiseaseLogs = judgeDiseaseLogMapper.queryJudgeDiseaseLog(judgeDiseaseLog);
		
		for(JudgeDiseaseLog dl : judgeDiseaseLogs) {
			Disease disease = diseaseMapper.selectByPrimaryKey(dl.getDeseaseId());
			dl.setDisease(disease);
		}
		
		judgeLog.setJudgeDiseaseLog(judgeDiseaseLogs);
		
		return judgeLog;
	}

	@Override
	public int updateMyJudgeLog(JudgeLog judgeLog) {
		return judgeLogMapper.updateMyJudgeLog(judgeLog);
	}

}
